//
// Created by Administrator on 2022/1/17.
//

#include "ImportedModel.h"

#include <fstream>
#include <sstream>
#include <string>
using namespace std;

ImportedModel::ImportedModel(const char* filePath)
{
    ModelImporter modelImporter;
    modelImporter.ParseOBJ(filePath);
    numVertices = modelImporter.GetNumVertices();
    std::vector<float> mVertices = modelImporter.GetVertices();
    std::vector<float> mUVs = modelImporter.GetUVs();
    std::vector<float> mNormals = modelImporter.GetNormals();
    for(int i = 0; i < numVertices; ++i)
    {
        vertices.emplace_back(glm::vec3(mVertices[i * 3], mVertices[i * 3 + 1], mVertices[i * 3 + 2]));
        uvs.emplace_back(glm::vec2(mUVs[i * 2], mUVs[i * 2 + 1]));
        normals.emplace_back(glm::vec3(mNormals[i * 3], mNormals[i * 3 + 1], mNormals[i * 3 + 2]));
    }
}

void ModelImporter::ParseOBJ(const char* filePath)
{
    float x, y, z;
    ifstream ifs(filePath, ios::in);
    string line = "";
    while(!ifs.eof())
    {
        getline(ifs, line);
        // 检测每一行开头的字符串
        // v行，
        if(line.compare(0, 2, "v ") == 0)
        {
            // 去掉这一行的开头的v
            stringstream ss(line.erase(0, 1));
            // ss的>>操作符会每次输出一个单词（去掉空格）
            ss >> x; ss >> y; ss >> z;
            vs.emplace_back(x);
            vs.emplace_back(y);
            vs.emplace_back(z);
        }
        // vt行
        if(line.compare(0, 2, "vt") == 0)
        {
            // 去掉这一行的开头的vt
            stringstream ss(line.erase(0, 2));
            // ss的>>操作符会每次输出一个单词（去掉空格）
            ss >> x; ss >> y;
            vts.emplace_back(x);
            vts.emplace_back(y);
        }
        // vn行
        if(line.compare(0, 2, "vn") == 0)
        {
            // 去掉这一行的开头的vn
            stringstream ss(line.erase(0, 2));
            // ss的>>操作符会每次输出一个单词（去掉空格）
            ss >> x; ss >> y; ss >> z;
            vns.emplace_back(x);
            vns.emplace_back(y);
            vns.emplace_back(z);
        }
        // f行
        if(line.compare(0, 2, "f ") == 0)
        {
            // 去掉这一行的开头的f
            stringstream ss(line.erase(0, 2));
            string s, v, t, n;
            for(int i = 0; i < 3; ++i)
            {
                getline(ss, s, ' ');
                stringstream sss(s);
                getline(sss, v, '/');
                getline(sss, t, '/');
                getline(sss, n, '/');
                // obj文件里索引从1开始的
                int vIndex = (stoi(v) - 1) * 3;
                int tIndex = (stoi(t) - 1) * 2;
                int nIndex = (stoi(n) - 1) * 3;
                vertices.emplace_back(vs[vIndex]);
                vertices.emplace_back(vs[vIndex + 1]);
                vertices.emplace_back(vs[vIndex + 2]);
                uvs.emplace_back(vts[tIndex]);
                uvs.emplace_back(vts[tIndex + 1]);
                normals.emplace_back(vns[nIndex]);
                normals.emplace_back(vns[nIndex + 1]);
                normals.emplace_back(vns[nIndex + 2]);
            }
        }
    }
}